module TranslitToHelper
  
  CharContainer=["a", "i", "s",'"',"b","j","t","y","v","k","u","'","g","l","f","è","d","m","h","û","e","n","c","â","ë","o","č","ž","p","š","z",
                  "r","ŝ"
                ]

    def translit_to(titel, *args)

    options = args.extract_options!
    translit_to_options = options.delete(:translit_to_style) || {}
    heading_options = options.delete(:heading) || {}
    heading_type = heading_options.delete(:type) || 'div'
    open_link_options = options.delete(:open_link) || {}
    close_link_options = options.delete(:close_link) || {}    
    content_options = options.delete(:content)|| {}   
    id_object = rand(100000000).to_s

    translit_open_link = "translit_open_link_#{id_object}"
    translit_close_link = "translit_close_link_#{id_object}"
    translit_content = "translit_form_#{id_object}"

    is_closed = options.delete(:is_closed)
    is_closed = true if is_closed.nil?
    open_js = "$('#{translit_content}').show(); $('#{translit_open_link}').hide(); $('#{translit_close_link}').show();"
    close_js = "$('#{translit_content}').hide(); $('#{translit_open_link}').show(); $('#{translit_close_link}').hide();"

    open_link = link_to_function(
      open_link_options.delete(:content) || "#{titel}",  "#{open_js}",
      :id => translit_open_link,
      :class => open_link_options.delete(:class) || "open_link",
      :style => "#{"display: none;" unless is_closed}#{open_link_options.delete(:style)}"
    )

    close_link = link_to_function(
      close_link_options.delete(:content) || "#{titel}", "#{close_js}",
      :id => translit_close_link,
      :class => close_link_options.delete(:class) || "close_link",
      :style => "#{"display: none;" if is_closed}#{close_link_options.delete(:style)}"
    )

    translit_hash = {"translit_class"=>translit_to_options.delete(:class),"translit_style"=>translit_to_options.delete(:style),
                     "heading_type"=>heading_type,"heading_class"=>heading_options.delete(:class),"open_link"=>open_link,"close_link"=>close_link,
                     "heading_content"=>heading_options.delete(:content),"content_class"=>content_options.delete(:class),
                     "translit_content"=>translit_content,"is_closed"=>is_closed,"content_style"=>content_options.delete(:style)
                    }

    concat(render :partial=>"#{@translit_to_config}/translit_to_form.html.erb", :locals=>{:translit_hash=>translit_hash})

  end


  def russian_keyboard
    chars = {"a"=>2,"и"=>20,"c"=>38,"ъ"=>56,"б"=>4,"й"=>22,"т"=>40,"ы"=>58,"в"=>6,"к"=>24,"у"=>42,"ь"=>60,"г"=>8,"л"=>26,"ф"=>44,
      "э"=>62,"д"=>10,"м"=>28,"х"=>46,"ю"=>64,"е"=>12,"н"=>30,"ц"=>48,"я"=>66,"ё"=>14,"о"=>32,"ч"=>50,"ж"=>16,"п"=>34,"ш"=>52,
      "з"=>18,"р"=>36,"щ"=>54
    }
    count = 0
    html="<table bgcolor=#FFFFFF><colgroup width=\"200\" span=\"8\"></colgroup><tr>"
    chars.each do |key, value|
      html += "<td width=\"40\" align=\"center\" style=\"background:#d6d1d1;font-size:22px;line-height:100%;text-decoration:none;\">
               <a href=\"javascript:put_char(#{value})\">#{key}</a></td>"
      html+="<td width=\"46\" align=\"center\" style=\"font-size:18px;\">#{CharContainer[count]}</td>"
      if count==23
        html+=" </tr>"
      end
      count += 1
      if count%4==0 and count<27
        html +=	"</tr><tr>"
      end
      if count%3==0 and count>=27
        html +=	"</tr>"
      end
    end
    html+="</table>"
    html
  end

end